package com.bawei.module_detail.adapter

import android.view.View
import android.widget.Toast
import com.bawei.module_detail.R
import com.bawei.module_detail.model.entity.CommentEntity
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

class CommentAdapter : BaseQuickAdapter<CommentEntity, CommentAdapter.CommentViewHolder>(R.layout.comment_item) {

    // 添加弹幕发送接口
    interface OnDanmakuListener {
        fun onSendDanmaku(text: String, isFlame: Boolean = false)
    }

    private var danmakuListener: OnDanmakuListener? = null


    private val list : List<String> = listOf(
        "https://c-ssl.dtstatic.com/uploads/blog/202204/24/20220424154356_e2a4c.thumb.400_0.jpg",
        "https://c-ssl.dtstatic.com/uploads/blog/202204/24/20220424154359_8f023.thumb.400_0.jpeg",
        "https://www.keaitupian.cn/cjpic/frombd/1/253/3942213212/2156467745.jpg",
        "https://c-ssl.dtstatic.com/uploads/blog/202301/20/20230120215421_30525.thumb.400_0.jpeg",
        "https://c-ssl.dtstatic.com/uploads/blog/202204/24/20220424154358_897b7.thumb.400_0.png",
        "https://c-ssl.dtstatic.com/uploads/blog/202204/24/20220424154400_76f8c.thumb.400_0.jpeg",
        "https://c-ssl.dtstatic.com/uploads/blog/202212/15/20221215011544_643be.thumb.400_0.jpg",
        "https://c-ssl.dtstatic.com/uploads/blog/202310/09/eASOl2nWUMl7BbZ.thumb.400_0.jpeg",
        "https://c-ssl.dtstatic.com/uploads/blog/202206/14/20220614153049_a96a0.thumb.400_0.jpg",
        "https://c-ssl.dtstatic.com/uploads/blog/202307/30/XxSyblBwiQNPv2J.thumb.400_0.png"
    )
    class CommentViewHolder(binding :  View) : BaseViewHolder(binding)

    override fun convert(holder: CommentViewHolder, item: CommentEntity) {
// 关键修改：根据position取模获取头像
        val avatarUrl = list[holder.adapterPosition % list.size]

        Glide.with(holder.itemView.context).load(avatarUrl).circleCrop().into(holder.getView(R.id.ivAvatar))

        holder.setText(R.id.tvComment, item.content)
        holder.setText(R.id.tvTime, item.createtime)
        holder.setText(R.id.tvLikeCount, item.agreenum.toString())
        holder.setText(R.id.tvUsername, item.nickname)

        // 添加长按事件，长按发送烈焰弹幕
        holder.getView<View>(R.id.tvComment).setOnLongClickListener {
            danmakuListener?.onSendDanmaku(item.content, true) // 烈焰弹幕
            Toast.makeText(holder.itemView.context, "长按发送烈焰弹幕：${item.content}", Toast.LENGTH_SHORT).show()
            true
        }
    }

    // 添加 setOnDanmakuListener 方法
    fun setOnDanmakuListener(listener: OnDanmakuListener) {
        this.danmakuListener = listener
    }
}


